1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * TimesInvocationHandler.java
6    *
7    * Created on 18-Jan-2005 at 9:22:20 AM
8    */
9   package ca.uhn.cache.jmock;
10  
11  import junit.framework.Assert;
12  
13  import org.jmock.core.Invocation;
14  import org.jmock.core.InvocationMatcher;
15  
16  
17  /***
18   * Matches when an invocation is executed the specified number of times. 
19   * 
20   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
21   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:54:12 $ by $Author: bryan_tripp $
22   */
23  class InvokeNumberOfTimesInvocationHandler implements InvocationMatcher {
24      
25      private final int myExpectedInvocationCount;
26      private int myInvocationCount;
27  
28      /***
29       * @param theExpectedInvocationCount
30       */
31      public InvokeNumberOfTimesInvocationHandler( int theExpectedInvocationCount ) {
32          myExpectedInvocationCount = theExpectedInvocationCount;
33          myInvocationCount = 0;
34      }
35  
36      /***
37       * {@inheritDoc}
38       */
39      public boolean matches( Invocation theInvocation ) {
40          return true;
41      }
42  
43      /***
44       * {@inheritDoc}
45       */
46      public void invoked( Invocation theInvocation ) {
47          myInvocationCount++;
48      }
49  
50      /***
51       * {@inheritDoc}
52       */
53      public boolean hasDescription() {
54          return true;
55      }
56  
57      /***
58       * {@inheritDoc}
59       */
60      public void verify() {
61          Assert.assertEquals( 
62                  "expected method was invoked " +myInvocationCount+ " times, " +
63                  "but it was expected to be invoked " +myExpectedInvocationCount+ " times", 
64                  myExpectedInvocationCount, 
65                  myInvocationCount );
66      }
67  
68      /***
69       * {@inheritDoc}
70       */
71      public StringBuffer describeTo( StringBuffer theBuffer ) {
72          theBuffer.append( "expected " +myExpectedInvocationCount+ " times, " );
73          theBuffer.append( "and has been invoked " +myInvocationCount+ " times" );
74          return theBuffer;
75      }
76  
77  }